package com.eco.fanliapp.controller.app;

import com.eco.fanliapp.common.base.BaseController;
import com.eco.fanliapp.common.response.ServerResponse;
import com.eco.fanliapp.dao.AppUserRedRewardMapper;
import com.eco.fanliapp.entity.AppUser;
import com.eco.fanliapp.service.ISyUserInvitationMoneyService;
import com.eco.fanliapp.service.RedisTemplateService;
import com.eco.fanliapp.vo.RedRewardInvitationListVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @date 2019-10-15
 */
@RestController
@RequestMapping("/app/user")
public class SyUserInvitationMoneyController extends BaseController {

    @Autowired
    private ISyUserInvitationMoneyService iSyUserInvitationMoneyService;
    @Autowired
    private AppUserRedRewardMapper appUserRedRewardMapper;
    @Autowired
    private RedisTemplateService redisTemplateService;

    /**
     *   邀请收益
     *   type 1查询两条，2查询全部
     */
    @GetMapping("/invite/makeMoney")
    @ResponseBody
    public ServerResponse invitationMakeMoney(@RequestParam(defaultValue = "1")Integer type){
        AppUser u = getAppUser();
        if(null == u) {
            return ServerResponse.createByErrorCodeMessage(301,"Token不正确");
        }
        return iSyUserInvitationMoneyService.invitationMakeMoney(u.getUserId(),type);
    }

    /**
     *   邀请收益描述
     *   type 1当前月，2上个月
     */
    @GetMapping("/invite/document")
    @ResponseBody
    public ServerResponse invitationDocument(@RequestParam(defaultValue = "1")Integer type){
        AppUser u = getAppUser();
        if(null == u) {
            return ServerResponse.createByErrorCodeMessage(301,"Token不正确");
        }
        return iSyUserInvitationMoneyService.invitationDocument(type);
    }

    /**
     *   邀请收益榜单
     *   1本月，2上月
     */
    @GetMapping("/invite/list")
    @ResponseBody
    public ServerResponse invitationList(@RequestParam(defaultValue = "1") Integer type){
        AppUser u = getAppUser();
        if(null == u) {
            return ServerResponse.createByErrorCodeMessage(301,"Token不正确");
        }
        return iSyUserInvitationMoneyService.invitationList(u.getUserId(),type);
    }

    /**
     * 邀请榜单假数据
     *
     */
    @GetMapping("/invite/fakeList")
    @ResponseBody
    public ServerResponse invitationFakeList(@RequestParam(defaultValue = "1") Integer type){
        AppUser u = getAppUser();
        if(null == u) {
            return ServerResponse.createByErrorCodeMessage(301,"Token不正确");
        }
        return iSyUserInvitationMoneyService.invitationFakeList(u.getUserId(),type);
    }

    /**
     * 邀请榜单假数据
     *
     */
    @GetMapping("/invite/addMoney")
    @ResponseBody
    public ServerResponse invitationAddMoney(){
        List<RedRewardInvitationListVo> list = appUserRedRewardMapper.findInvitationFakeList(1);
        for (RedRewardInvitationListVo redVo:list
             ) {
//          BigDecimal money = redVo.getTotalAmount();
//          double d = Math.random();
//          int  i = (int)(30*Math.random()+1);
//          money = money.add(BigDecimal.valueOf(d)).add(BigDecimal.valueOf(i));
//          appUserRedRewardMapper.updateFakeList(redVo.getUserId(),money);
        }
        return null;
    }

    /**
     * 邀请人数榜单
     *
     */
    @GetMapping("/invite/fansNumber")
    @ResponseBody
    public ServerResponse invitationfansNumber(Integer month){
        if(month >=1 && month <=12){
            Calendar cale = null;
            cale = Calendar.getInstance();
            int num = cale.get(Calendar.MONTH) + 1;
            int day = cale.get(Calendar.DATE);
            List<RedRewardInvitationListVo> listVoList= null;
            if(month == num){
                String key = "app_user_invite_fansNum_list_day_"+day;
                if(redisTemplateService.hasKey(key)){
                    listVoList = redisTemplateService.getTByListKey(key,RedRewardInvitationListVo.class);
                }
                if(listVoList == null){
                    listVoList= appUserRedRewardMapper.invitationfansNumber(month, 1);
                    redisTemplateService.set(key,listVoList,60*5);
                }
            }else {
                String key = "app_user_invite_fansNum_list_month_"+month;
                if(redisTemplateService.hasKey(key)){
                    listVoList = redisTemplateService.getTByListKey(key,RedRewardInvitationListVo.class);
                }
                if(listVoList == null){
                    listVoList= appUserRedRewardMapper.invitationfansNumber(month, 1);
                    redisTemplateService.set(key,listVoList,60*60*24);
                }
            }
            return ServerResponse.createBySuccess(listVoList);
        }
        return ServerResponse.createByErrorMessage("参数有误");
    }
}
